﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.Common;

namespace DAL
{
    public class KeysCategoryDAL : AbstractDAL<KeysCategory>
    {
        static readonly string INSERT = "insert into KeysCategory values(null,@KID,@CID);select last_insert_rowid() newid;";
        static readonly string UPDATE = "update KeysCategory set KID=@KID,CID=@CID where id=@ID";
        static readonly string DELETE = "delete from KeysCategory where id=@ID";
        static readonly string DELETE_BY_KID = "delete from KeysCategory where KID=@KID";
        static readonly string SELECT = "select * from  KeysCategory where id=@ID";
        static readonly string SELECT_ALL = "select * from  KeysCategory";

        protected override string InsertCommand
        {
            get
            {
                return INSERT;
            }
        }

        protected override string UpdateCommand
        {
            get { return UPDATE; }
        }

        protected override string DeleteCommand
        {
            get { return DELETE; }
        }

        protected override string SelectCommand
        {
            get { return SELECT; }
        }

        protected override string SelectAllCommand
        {
            get { return SELECT_ALL; }
        }

        protected override DbParameter[] GetInsertParameter(KeysCategory model)
        {
            return new DbParameter[] { SqlHelper.CreateParameter("@KID", model.KID), SqlHelper.CreateParameter("@CID", model.CID) };
        }

        protected override void SyncID(KeysCategory model, object scalar)
        {
            model.ID = (long)scalar;
        }

        protected override DbParameter[] GetUpdateParameter(KeysCategory model)
        {
            return new DbParameter[] { SqlHelper.CreateParameter("@KID", model.KID), SqlHelper.CreateParameter("@CID", model.CID) };
        }

        protected override DbParameter[] GetDeleteParameter(KeysCategory model)
        {
            return new DbParameter[] { SqlHelper.CreateParameter("@ID", model.ID) };
        }

        protected override DbParameter[] GetSeleteParameter(long id)
        {
            return new DbParameter[] { SqlHelper.CreateParameter("@ID", id) };
        }

        protected override KeysCategory GetModel(DbDataReader reader)
        {
            KeysCategory entity = new KeysCategory();
            entity.ID = (long)reader["ID"];
            entity.KID = (long)reader["KID"];
            entity.CID = (long)reader["CID"];
            return entity;
        }

        public void DeleteAllByKID(long kid)
        {
            DbCommand command = null;
            try
            {
                command = SqlHelper.GetCommand(DELETE_BY_KID);
                DbParameter parameters = SqlHelper.CreateParameter("@KID", kid);
                command.Parameters.Add(parameters);
                command.ExecuteNonQuery();
            }
            finally
            {
                SqlHelper.Close(command);
            }
        }
    }
}
